import telnetAPI
import time
import argparse

def parse_args():
    parser = argparse.ArgumentParser(description="you should add those parameter") 
    parser.add_argument('--task_id')
    parser.add_argument('--switchIp',default="10.174.216.35")
    parser.add_argument('--source',default="192.168.1.4")      
    parser.add_argument('--destination',default="192.168.6.4")
    parser.add_argument('--inport', default="100GE1/0/1")
    parser.add_argument('--outport', default="100GE1/0/17")
    parser.add_argument('--aclId', default="410")
    parser.add_argument('--onlydel', default="False")
    args = parser.parse_args()       
    return args


def publish_acl_no_small_clos():
    #host_ip = '10.174.216.35'
    args = parse_args()
    host_ip = args.switchIp
    onlydel = args.onlydel
    username = 'admin1234'
    password = 'Oxc_2012'
    changepwd = 'N'

    commands = []
    commit = 'commit'
    quit = 'q'
    sy = "system-view"
    admin = "admin"
    port_mode = "port-mode port"
    confirm = "Y"

    acl_to_publish = {}
    #acl_to_publish["vs1"] = [("192.168.6.3", None, "100GE1/0/49", "100GE1/0/1", "410"), ("192.168.7.3", None, "100GE1/0/50", "100GE1/0/2", "411")]
    acl_to_publish["vs1"] = [(args.source, args.destination, args.outport, args.inport, args.aclId)]
    # print(host_ip,acl_to_publish,onlydel)

    task_int_acl_map = {}
    task_id = args.task_id
    if onlydel == "False":
        for vs_name in acl_to_publish.keys():
            commands.append(sy)

            for entry in acl_to_publish[vs_name]:
                mark = entry[-1]
                src__ip = entry[0]
                dst_ip = entry[1]
                interface = entry[2]
                start_port = entry[3]

                commands.append("interface %s" % start_port)
                commands.append("acl number 3%s" % mark)
                cmd = "rule permit ip "
                if src__ip != None:
                    cmd += ("source %s 0" % src__ip)
                if dst_ip != None:
                    cmd += ("destination %s 0" % dst_ip)
                commands.append(cmd)
                commands.append(commit)
                commands.append(quit)

                commands.append("interface %s" % start_port)
                commands.append("traffic classifier c%s" % mark)
                commands.append("if-match acl 3%s" %mark)
                commands.append(commit)
                commands.append(quit)

                commands.append("interface %s" % start_port)
                commands.append("traffic behavior b%s" % mark)
                commands.append("redirect nexthop %s" %interface)
                commands.append(commit)
                commands.append(quit)

                commands.append("interface %s" % start_port)
                commands.append("traffic policy p%s" % mark)
                commands.append("classifier c%s behavior b%s" %(mark, mark))
                commands.append(commit)
                commands.append(quit)

                commands.append("interface %s" % start_port)
                commands.append("traffic-policy p%s inbound" % mark)
                commands.append(commit)
                if task_id not in task_int_acl_map:
                    task_int_acl_map[task_id] = []
                task_int_acl_map[task_id].append((start_port, mark))
            commands.append(quit)
    else:
        commands.append(sy)
        for acl_pair in task_int_acl_map[task_id]:
            to_del_int = acl_pair[0]
            to_del_acl_id = acl_pair[1]
            commands.append("interface %s" % to_del_int)
            commands.append("undo traffic-policy p%s inbound" %to_del_acl_id)
            commands.append(commit)
            commands.append(quit)

            commands.append("undo traffic policy p%s" %to_del_acl_id)
            commands.append(commit)

            commands.append("undo traffic behavior b%s" %to_del_acl_id)
            commands.append(commit)

            commands.append("undo traffic classifier c%s" %to_del_acl_id)
            commands.append(commit)

            commands.append("undo acl 3%s" %to_del_acl_id)
            commands.append(commit)
        del task_int_acl_map[task_id]
        commands.append(quit)
        
    telnet_client = telnetAPI.TelnetClient()
    if telnet_client.login_host(host_ip, username, password, changepwd):
        for cmd in commands:
            telnet_client.execute_some_command(cmd, interval=0.1)
    else:
        print("fail")
        time.sleep(3)

if __name__ == "__main__":
    publish_acl_no_small_clos()


